package cn.b.sky.mes.qm.service.impl;

import java.util.List;

import cn.b.sky.mes.qm.service.DefectNodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.common.utils.TreeUtil;
import cn.b.sky.mes.qm.dao.DefectNodeDao;
import cn.b.sky.mes.qm.model.DefectNode;

@Service("defectNodeService")
public class DefectNodeServiceImpl implements DefectNodeService {

  @Autowired
  private DefectNodeDao defectNodeDao;

  @Override
  public List<TreeInterface> getDefectNodeTree(String siteCode) {
    if (StringUtils.isBlank(siteCode)) {
      return null;
    }
    List<DefectNode> nodes = defectNodeDao.selectAllDefectNodeBySiteCode(siteCode);
    return TreeUtil.gradingRecursion(nodes.toArray(new DefectNode[nodes.size()]), null);
  }

  @Override
  public void saveDefectNode(DefectNode defectNode) {
    if (defectNode != null) {
      this.defectNodeDao.insertDefectNode(defectNode);
    }

  }

  @Override
  public DefectNode getDefectNodeByNodeCodeAndSiteCode(String siteCode, String nodeCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(nodeCode)) {
      return null;
    }
    return defectNodeDao.selectDefectNodeByPk(siteCode, nodeCode);
  }

  @Override
  public void deleteDefectNodeByNodeCodeAndSiteCode(String siteCode, String nodeCode) {
    if (!StringUtils.isBlank(siteCode) && !StringUtils.isBlank(nodeCode)) {
      defectNodeDao.deleteDefectNodeByPk(siteCode, nodeCode);
    }
  }

  @Override
  public List<DefectNode> getChildrensNode(String siteCode, String parentNodeCode) {
    if (!StringUtils.isBlank(siteCode) && !StringUtils.isBlank(parentNodeCode)) {
      return defectNodeDao.selectChildrensNode(siteCode, parentNodeCode);
    }
    return null;
  }

  @Override
  public void modifyDefectNode(DefectNode defectNode) {
    if (defectNode != null && !StringUtils.isBlank(defectNode.getNodeCode())) {
      defectNodeDao.updateDefectNode(defectNode);
    }

  }

}
